package com.mongoproject.database;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.*;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

/**
 * This class the main GUI class of the program. It acts the parser between the client
 * and the server.
 * @author Xuejian Sun
 *
 */
@SuppressWarnings("serial")
public class QueryUI extends JFrame{
	ArrayList<String> list;
	JMenuItem exit=new JMenuItem("Exit");
	JMenuItem readme=new JMenuItem("ReadMe");
	JFrame frame = new JFrame ();
	JPanel panel1=new JPanel(new BorderLayout());
	JPanel panel2=new JPanel(new BorderLayout());
	JPanel panel3=new JPanel(new BorderLayout());
	JPanel panel4=new JPanel(new BorderLayout());
	JPanel panel5=new JPanel(new BorderLayout());
	JPanel panel6=new JPanel(new BorderLayout());
	JPanel panel7=new JPanel(new BorderLayout());
	JComboBox comboBox = new JComboBox();
	JTextArea textarea = new JTextArea(10, 1);
	JTextArea textareaup =new JTextArea(11, 1);
	JTextField database =new JTextField(8);
	JTextField collection =new JTextField(8);
	JLabel databaseLabel =new JLabel("Database");
	JLabel collectionLabel =new JLabel("Collection");
	JLabel actionLabel =new JLabel("Action");
	JLabel resultLabel =new JLabel("Result");
	JScrollPane sbrText;
	
	public QueryUI() {
		textarea.setWrapStyleWord(true);
		textarea.setLineWrap(true);
		textareaup.setWrapStyleWord(true);
		textareaup.setLineWrap(true);
	

		BorderLayout border=new BorderLayout();
		frame.setLayout(border);
		
		//panel1.setLayout(border);
	    JMenu menus=new JMenu("File");
	    JMenu menus2=new JMenu("Help");
	    menus.add(exit);
	    menus2.add(readme);
	    JMenuBar mb= new JMenuBar();
	    frame.setJMenuBar(mb);
	    mb.add(menus);
	    mb.add(menus2);
	 
	    panel4.add(collectionLabel,BorderLayout.NORTH);
	    panel4.add(collection,BorderLayout.LINE_START);
	    
	    panel3.add(textareaup,BorderLayout.CENTER);
	    
	    panel5.add(databaseLabel,BorderLayout.NORTH);
	    panel5.add(database,BorderLayout.LINE_START);
	    
	    panel7.add(actionLabel,BorderLayout.NORTH);
	    panel7.add(comboBox,BorderLayout.LINE_START);
	    
	    panel6.add(panel5, BorderLayout.LINE_START);
	    panel6.add(panel4,BorderLayout.CENTER);
	    panel6.add(panel7,BorderLayout.LINE_END);
	    

	    comboBox.addItem("Insert");
	    comboBox.addItem("Update");
	    comboBox.addItem("Delete");
	    comboBox.addItem("Find");
	    comboBox.addItem("Generate");
	    JButton button2 =new JButton ("Execute");
	    panel2.add(resultLabel, BorderLayout.LINE_START);
	    panel2.add(button2, BorderLayout.LINE_END);
	    
	    panel1.add(panel2,BorderLayout.NORTH);
	    sbrText = new JScrollPane(textarea);
	    panel1.add(sbrText,BorderLayout.SOUTH);
	    
	    frame.add(panel3,BorderLayout.CENTER);
	    frame.add(panel1,BorderLayout.SOUTH);
	    frame.add(panel6,BorderLayout.NORTH);
	    
	    exitButton exiton=new exitButton();
	    exit.addActionListener(exiton);
	    
	    execButton exec =new execButton();
	    
	    button2.addActionListener(exec);
	    
	    frame.setTitle("View");
        frame.setSize(600, 450);
        frame.setLocation(100, 100);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	  }

	public static void main(String[] str) throws IOException{
		QueryUI f = new QueryUI(); //Create a window
		
	}
	
	/**
	 * exit the program 
	 *
	 */
	public class exitButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object objSource = e.getSource();
			if(objSource.equals(exit)) {
				System.exit(0);		
			}
			
		}
		
	}
	/**
	 * execute the program
	 * Store the Database name, Collection name, the action and query into an array.
	 */
	public class execButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String result = "";
			list=new ArrayList<String>();
			String data=database.getText();
			String coll=collection.getText();
			String act=comboBox.getSelectedItem().toString();
			String query=textareaup.getText();
			list.add(data);
			list.add(coll);
			list.add(act);
			list.add(query);

			QueryProcesser p2= new QueryProcesser(list);
			
			try {
				result = p2.performQuery();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			//Test use
			textarea.setText(result);
			//System.out.println("datanase: "+data+" collection: "+coll+ " action: "+ act+ " query "+query);
			
		}
		
	}
	/**
	 * This function return the array that contains the information of the query
	 * @return the array list
	 */
	public ArrayList<String>  getArray(){
		return list;
		
	}

}
